// pages/member/course/CourseIndex/CourseIndex.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    recGoodList: [],
    hotGoodList: [],
    newGoodList: [],
    // recGoodList: [{
    // 	"id": 104,
    // 	"goods_id": 227,
    // 	"lecturer_id": 1,
    // 	"title": "\u534e\u4fa8\u5e0133",
    // 	"thumb": "https:\/\/dev5.yunzshop.com\/attachment\/images\/2\/2018\/08\/L8Ia553RrZH83rEiLocYIY3L9o5885.jpg",
    // 	"price": "198.00",
    // 	"has_one_lecturer": {
    // 		"id": 1,
    // 		"real_name": "1",
    // 		"mobile": "15219025049"
    // 	},
    // 	"course_chapter_num": 1
    // }, {
    // 	"id": 93,
    // 	"goods_id": 206,
    // 	"lecturer_id": 3,
    // 	"title": "3333",
    // 	"thumb": "https:\/\/dev5.yunzshop.com\/attachment\/images\/2\/2018\/08\/L8Ia553RrZH83rEiLocYIY3L9o5885.jpg",
    // 	"price": "100.00",
    // 	"has_one_lecturer": {
    // 		"id": 3,
    // 		"real_name": "\u6d4b\u8bd5",
    // 		"mobile": "13800138000"
    // 	},
    // 	"course_chapter_num": 1
    // }],
    imgUrls: [],
    showCarousel: false,
    activeIndex: 0,
    listMarginLeft: 0,
    page: 1,
    loading: true,

    Height: 0,
    LastHeight: 0,
    share_name:"",
    share_desc:"",
    share_cover:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getSlide();
    this._getData('is_recommand');
    this._getData('is_hot');
    this._getData('');
    this.getCarouselInfo();
  },
  tapNav(e) {
    console.log(e);
    let link = e.currentTarget.dataset.link;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) { console.log(err); }
  },
  gotoSearch(e) {
    console.log(e.currentTarget.dataset.type);
    let typess = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/packageA/member/course/CourseSearch/CourseSearch?goods_type=' + typess,
    });
  },
  recommend_startBtn(e) {
    this.setData({
      startX: e.changedTouches[0].pageX
    });
  },
  recommend_moveBtn(e) {
    this.setData({
      endX: e.changedTouches[0].pageX
    });
  },
  recommend_endBtn(e) {
    var distanceX = this.data.endX - this.data.startX;
    if (this.data.activeIndex >= 0 && distanceX > 0) {
      this.data.listMarginLeft = this.data.listMarginLeft - 480;
      this.data.activeIndex = this.data.activeIndex - 1;
      this.setData({
        listMarginLeft: this.data.listMarginLeft,
        activeIndex: this.data.activeIndex
      });
      if (this.data.listMarginLeft <= 0 || this.data.activeIndex <= 0) {
        this.setData({
          listMarginLeft: 0,
          activeIndex: 0
        });
      }
    } else if (this.data.activeIndex < this.data.recGoodList.length - 1 && distanceX < 0) {
      this.setData({
        listMarginLeft: this.data.listMarginLeft + 480,
        activeIndex: this.data.activeIndex + 1
      });
    }
    console.log(distanceX, this.data.activeIndex, this.data.activeIndex, this.data.listMarginLeft);
  },
  //获取轮播和导航
  getCarouselInfo() {
    var that = this;
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-demand.get-video-info"),
      success: function (res) {
        if (res.data.result == 1) {
          if (!app._isTextEmpty(res.data.data.slide)) {
            that.setData({
              showCarousel: true,
              carouselInfo: res.data.data.slide
            });
          }
          if (!app._isTextEmpty(res.data.data.navigation)) {
            that.setData({
              showCarousel: true,
              navigation: res.data.data.navigation
            });
          }
          let {share} = res.data.data;
          if(!app._isTextEmpty(share)){
            that.setData({
              share_name:share.share_name,
              share_desc:share.share_desc,
              share_cover:share.share_cover
            })
          }
          console.log(res.data.data.slide);
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  _getSlide: function (success) {
    var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-demand.get-video-slide"),
      success: function (res) {
        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          let _data = res.data.data;
          that.setData({
            imgUrls: _data
          });
          success && success(res);
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  _getData(type) {
    var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods&in_home_page=1"),
      data: {
        goods_type: type,
      },
      success: function (res) {
        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          let _data = res.data.data;
          switch (type) {
          case "is_recommand":
            that.setData({
              recGoodList: _data.data
            });
            break;
          case "is_hot":
            that.setData({
              hotGoodList: _data.data
            });
            break;
          case "":
            that.setData({
              newGoodList: _data.data,
              current_page: _data.current_page,
              last_page: _data.last_page
            });
            break;
          default:
            break;
          }

        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  toCourse: function (e) {
    console.log(e.currentTarget.dataset.id);
    let itemid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + itemid
      // url: '/packageA/detail_v2/detail_v2?id=' + itemid
    });
  },
  moreCourse(e) {
    let type = e.currentTarget.dataset.type;
    if (type == 'all') {
      wx.navigateTo({
        url: '/packageA/member/course/CourseSearch/CourseSearch?goods_type=' + type
      });
    } else {
      if (this.data.current_page >= this.data.last_page) {
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
          duration: 1000
        });
        this.setData({
          loading: false
        });
        return;
      } else {
        let is_page = this.data.page + 1;
        this.setData({
          page: is_page
        });
        console.log(is_page);
        this.getMore(is_page);
      }
    }
  },
  _getGood: function (id) {
    // var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods-detail"),
      data: {
        goods_id: id
      },
      success: function (res) {
        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          // let _data = res.data.data;
          wx.navigateTo({
            url: '/packageA/detail_v2/detail_v2?id=' + itemid
          });
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  getMore(is_page) {
    var that = this;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods&in_home_page=1"),
      data: {
        goods_type: '',
        page: is_page,
        search: '',
      },
      success: function (res) {
        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          let _data = res.data.data;
          that.setData({
            newGoodList: that.data.newGoodList.concat(_data.data),
            current_page: _data.current_page,
            last_page: _data.last_page
          });
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
    wx.setNavigationBarTitle({
      title: '课程'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title: this.data.share_name,
      desc: this.data.share_desc,
      imageUrl:this.data.share_cover
    }
  }
});
